<?php

class ChannelsController extends Controller_Action
{
    /**
     * @var Model\Users
     */
    private $mUsers     = NULL;
    /**
     * @var Model\channels
     */
    private $mChannels  = NULL;
    
    public function init()
    {
        parent::init();
        
        // i18n
        \Helper\loadI18n('channels', 'main');
        
        // models
        $this->mUsers = new Model\Users();
        
        \Helper\loadModel('channels', 'channels');
        $this->mChannels = new Model\channels();
    }

    public function indexAction($msgSender = NULL, $msgValue = NULL)
    {   
        $channelsInfo =
        $this->mChannels->ctrlGetChannelsInfo($this->userId);
        
        $this->view->channelsInfo = $channelsInfo;
        
        if ($msgSender !== NULL && $msgValue !== NULL)
        {
            $this->view->msgSender  = $msgSender;
            $this->view->msgValue   = $msgValue;
        }
    }
    
    /**
     * facebook Sign in
     * 
     * @param string $code 
     */
    public function callbackFacebookAction($code = NULL)
    {
        $msgError = NULL;
        
        $oFacebook  = new cAppFacebook(dtoFacebookAuth::factory());
        try
        {
            $ac = $oFacebook->getAccessToken($code);
            if ($ac === NULL)
            {
                $rurl = $oFacebook->getRedirectUrl();
                header("Location: {$rurl}");
                exit(0);
            }
            else
            {
                // successful processing, access token ready ($ac)
                $this->mChannels->addChannelFacebook($this->userId, $ac->oauth_token);
            }
        }
        catch (Exception $e)
        {
            $msgError = $e->getMessage();
        }
        
        $this->_helper->redirector("index");
    }
}